package etc.com.market;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.content.Intent;

public class SenmailActivity extends Activity {

	private static final int DELETE_WORK = Menu.FIRST;

	private EditText workEnter;
	private EditText title;
	private EditText content;
	private Button button;

	ArrayList<Work> array;
	ListWorkAdapter arrayAdapter;
	private static final int REQUEST_CODE = 10;
	String storename;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.sendmail);

		array = new ArrayList<Work>();
		arrayAdapter = new ListWorkAdapter(this, R.layout.list, array);

		workEnter = (EditText) findViewById(R.id.work_enter);
		title = (EditText) findViewById(R.id.title);
		content = (EditText) findViewById(R.id.content);
		
		Bundle extras = getIntent().getExtras();
		if (extras == null) {
			return;
		}

		String messageReceive = extras.getString("message");

		if (messageReceive != null) {
			storename = messageReceive.toString();
		}

		final Button button = (Button) findViewById(R.id.button);

		final ListView list = (ListView) findViewById(R.id.list);
		list.setAdapter(arrayAdapter);

		OnClickListener add = new OnClickListener() {
			public void onClick(View v) {
				if (workEnter.getText().toString().equals("")
						|| title.getText().toString().equals("")
						|| content.getText().toString().equals("")) {
					AlertDialog.Builder builder = new AlertDialog.Builder(
							SenmailActivity.this);
					builder.setTitle("Info missing");
					builder.setMessage("Please enter all information of the work");
					builder.setPositiveButton("Continue",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									// TODO Auto-generated method stub
								}
							});
					builder.show();
				} else {

					// GmailSender sender = new GmailSender(workEnter.getText()
					// .toString(), "");

					GmailSender sender = new GmailSender("cancubuthongmanh@gmail.com", "Dung140182");
					try {
						sender.sendMail(title.getText().toString(), content
								.getText().toString(), workEnter.getText()
								.toString(), "ntdan.globaltech@gmail.com, nohara@wacontre.com"); // email
																			// gui
																			// den
																			// mac
																			// dinh
																			// la
																			// Tiengk2406....,
					} catch (Exception e) { // anh thay doi email de test
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

					workEnter.setText("");
					title.setText("");
					content.setText("");
					AlertDialog.Builder builder = new AlertDialog.Builder(
							SenmailActivity.this);
					builder.setTitle("Info");
					builder.setMessage("Successfull !");
					builder.setPositiveButton("Continue",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									Send();
								}
							});
					builder.show();
				}
			}

		};

		button.setOnClickListener(add);
	}

	public void Cancel(View view) {
		onClickCancel(view, (storename));
	}
	
	public void Send() {
		onClickSend(("店舗詳細;" + storename));
	}

	public void onClickCancel(View view, String message) {
		Intent i = new Intent(this, StoreTopPage.class);
		i.putExtra("message", message);
		// Set the request code to any code you like, you can identify the
		// callback via this code
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}
	
	public void onClickSend(String message) {
		Intent i = new Intent(this, SearchOursProducts.class);
		i.putExtra("message", message);
		// Set the request code to any code you like, you can identify the
		// callback via this code
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, DELETE_WORK, 0, "Delete").setIcon(
				android.R.drawable.ic_delete);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case DELETE_WORK: {
			deleteCheckedWork();
			break;
		}
		}
		return true;
	}

	private void deleteCheckedWork() {
		if (array.size() > 0) {
			for (int i = 0; i < array.size(); i++) {
				if (i > array.size()) {
					break;
				}

			}
		}
	}

}